package com.jade.editor.object.editors.parts;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Spinner;

public class SettingsObject extends Dialog {
	
	private RGB _borderColor = null;
	private RGB _selectionColor = null;
	private RGB _connectionColor = null;
	private RGB _fillColor = null;
	private RGB _poleColor = null;
	private RGB _poleSelectionColor = null;
	private boolean _isFilled = false;
	private int _xSize = 100;
	private int _ySize = 100;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsObject(Shell parentShell, RGB borderColor, RGB selectionColor, RGB connectionColor, RGB fillColor, RGB poleColor, RGB poleSelectionColor, boolean isFilled, int xSize, int ySize) {
		super(parentShell);
		_borderColor = borderColor;
		_selectionColor = selectionColor;
		_fillColor = fillColor;
		_connectionColor = connectionColor;
		_isFilled = isFilled;
		_poleColor = poleColor;
		_poleSelectionColor = poleSelectionColor;
		_xSize = xSize;
		_ySize = ySize;
	}
	
	public RGB getBorderColor(){
		return _borderColor;
	}
	
	public RGB getSelectionColor(){
		return _selectionColor;
	}
	
	public RGB getConnectionColor(){
		return _connectionColor;
	}
	
	public RGB getFilledColor(){
		return _fillColor;
	}
	
	public RGB getPoleColor(){
		return _poleColor;
	}
	
	public RGB getPoleSelectionColor(){
		return _poleSelectionColor;
	}
	
	public boolean getFilled(){
		return _isFilled;
	}
	
	public Point getSize(){
		return new Point(_xSize, _ySize);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		Label lblBorderColor = new Label(container, SWT.NONE);
		lblBorderColor.setBounds(10, 10, 107, 15);
		lblBorderColor.setText("Border Color:");
		
		Label lblSelectColor = new Label(container, SWT.NONE);
		lblSelectColor.setBounds(10, 31, 107, 15);
		lblSelectColor.setText("Selection Color:");
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setBounds(10, 73, 107, 15);
		lblNewLabel.setText("Fill Color:");
		
		Label lblConnectionColor = new Label(container, SWT.NONE);
		lblConnectionColor.setBounds(10, 52, 107, 15);
		lblConnectionColor.setText("Connection Color:");
		
		Button btnCheckButton = new Button(container, SWT.CHECK);
		btnCheckButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_isFilled = _isFilled ? false : true;
			}
		});
		btnCheckButton.setBounds(10, 172, 194, 16);
		btnCheckButton.setSelection(_isFilled);
		btnCheckButton.setText("Fill if all nodes are connected");
		
		final Composite borderColor = new Composite(container, SWT.NONE);
		borderColor.setBackground(SWTResourceManager.getColor(_borderColor));
		borderColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(borderColor.getShell());
				RGB tmp = _borderColor;
				_borderColor = cd.open();
				if(_borderColor != null)
					borderColor.setBackground(SWTResourceManager.getColor(_borderColor));
				else
					_borderColor = tmp;
			}
		});
		borderColor.setBounds(140, 10, 294, 15);
		
		final Composite selectionColor = new Composite(container, SWT.NONE);
		selectionColor.setBackground(SWTResourceManager.getColor(_selectionColor));
		selectionColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(selectionColor.getShell());
				RGB tmp = _selectionColor;
				_selectionColor = cd.open();
				if(_selectionColor != null)
					selectionColor.setBackground(SWTResourceManager.getColor(_selectionColor));
				else
					_selectionColor = tmp;
			
			}
		});
		selectionColor.setBounds(140, 31, 294, 15);
		
		final Composite connectionColor = new Composite(container, SWT.NONE);
		connectionColor.setBackground(SWTResourceManager.getColor(_connectionColor));
		connectionColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(connectionColor.getShell());
				RGB tmp = _connectionColor;
				_connectionColor = cd.open();
				if(_connectionColor != null)
					connectionColor.setBackground(SWTResourceManager.getColor(_connectionColor));
				else
					_connectionColor = tmp;
			}
		});
		connectionColor.setBounds(140, 52, 294, 15);
		
		final Composite fillColor = new Composite(container, SWT.NONE);
		fillColor.setBackground(SWTResourceManager.getColor(_fillColor));
		fillColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(fillColor.getShell());
				RGB tmp = _fillColor;
				_fillColor = cd.open();
				if(_fillColor != null)
					fillColor.setBackground(SWTResourceManager.getColor(_fillColor));
				else
					_fillColor = tmp;
			}
		});
		fillColor.setBounds(140, 73, 294, 15);
		
		Label lblPoleColor = new Label(container, SWT.NONE);
		lblPoleColor.setBounds(10, 104, 107, 15);
		lblPoleColor.setText("Pole Color:");
		
		Label lblPoleSelectionColor = new Label(container, SWT.NONE);
		lblPoleSelectionColor.setBounds(10, 125, 119, 15);
		lblPoleSelectionColor.setText("Pole Selection Color:");
		
		final Composite poleColor = new Composite(container, SWT.NONE);
		poleColor.setBackground(SWTResourceManager.getColor(_poleColor));
		poleColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(poleColor.getShell());
				RGB tmp = _poleColor;
				_poleColor = cd.open();
				if(_poleColor != null)
					poleColor.setBackground(SWTResourceManager.getColor(_poleColor));
				else
					_poleColor = tmp;
			}
		});
		poleColor.setBounds(140, 102, 294, 15);
		
		final Composite poleSelectionColor = new Composite(container, SWT.NONE);
		poleSelectionColor.setBackground(SWTResourceManager.getColor(_poleSelectionColor));
		poleSelectionColor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ColorDialog cd = new ColorDialog(poleSelectionColor.getShell());
				RGB tmp = _poleSelectionColor;
				_poleSelectionColor = cd.open();
				if(_poleSelectionColor != null)
					poleSelectionColor.setBackground(SWTResourceManager.getColor(_poleSelectionColor));
				else
					_poleSelectionColor = tmp;
			}
		});
		poleSelectionColor.setBounds(140, 125, 294, 15);
		
		final Spinner xSpinner = new Spinner(container, SWT.BORDER);
		xSpinner.setBounds(275, 182, 60, 22);
		xSpinner.setMinimum(100);
		xSpinner.setMaximum(99999999);
		xSpinner.setSelection(_xSize);
		xSpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_xSize = xSpinner.getSelection();
			}
		});
		
		final Spinner ySpinner = new Spinner(container, SWT.BORDER);
		ySpinner.setBounds(374, 182, 60, 22);
		ySpinner.setMinimum(100);
		ySpinner.setMaximum(99999999);
		ySpinner.setSelection(_ySize);
		ySpinner.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				_ySize = ySpinner.getSelection();
			}
		});
		
		Label xLabel = new Label(container, SWT.NONE);
		xLabel.setBounds(233, 185, 36, 15);
		xLabel.setText("Size X:");
		
		Label yLabel = new Label(container, SWT.NONE);
		yLabel.setBounds(355, 185, 16, 15);
		yLabel.setText("Y:");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 289);
	}
}
